package com.shungen.apicloud.controller;

import com.shungen.apicloud.annotation.AuthCheck;
import com.shungen.apicloud.common.BaseResponse;
import com.shungen.apicloud.common.ErrorCode;
import com.shungen.apicloud.common.ResultUtils;
import com.shungen.apicloud.constant.UserConstant;
import com.shungen.apicloud.exception.ThrowUtils;
import com.shungen.apicloud.model.dto.interfacelabel.InterfaceLabelAddRequest;
import com.shungen.apicloud.model.entity.InterfaceLabel;
import com.shungen.apicloud.service.InterfaceLabelService;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shungen
 * @date 2024年08月05日 23:29
 */

@RestController
@RequestMapping("/label")
public class InterfaceLabelController {

    @Resource
    private InterfaceLabelService interfaceLabelService;


    /**
     * 查询所有的标签
     * @return
     */
    @GetMapping("/all")
    public List<InterfaceLabel> getAllInterfaceLabel(){
        return interfaceLabelService.getAllInterfaceLabel();
    }

    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> addInterfaceLabel(@RequestBody InterfaceLabelAddRequest interfaceLabelAddRequest){
        ThrowUtils.throwIf(interfaceLabelAddRequest == null || interfaceLabelAddRequest.getLabelName() == null,
                ErrorCode.PARAMS_ERROR,"请求参数错误");
        InterfaceLabel interfaceLabel = new InterfaceLabel();
        BeanUtils.copyProperties(interfaceLabelAddRequest,interfaceLabel);
        boolean save = interfaceLabelService.save(interfaceLabel);
        ThrowUtils.throwIf(!save,ErrorCode.OPERATION_ERROR,"新增标签失败");
        return ResultUtils.success(true);
    }
}
